package android.tusnotas.model;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;
import android.tusnotas.beans.Grade;

public class GradeModel {

	
//INSERTS
	public long insertGrade(String activity, String grade, Integer courseId) {
		TusNotasModel.gradeInsertStmt.bindString(1, activity);
		TusNotasModel.gradeInsertStmt.bindString(2, grade);
		TusNotasModel.gradeInsertStmt.bindDouble(3, courseId);
		return TusNotasModel.gradeInsertStmt.executeInsert();
	}
	
	public long insertGrade(Grade grade) {
		TusNotasModel.gradeInsertStmt.bindString(1, grade.getActivity());
		TusNotasModel.gradeInsertStmt.bindString(2, grade.getGrade());
		TusNotasModel.gradeInsertStmt.bindDouble(3, grade.getCourseId());
		try{
			return TusNotasModel.gradeInsertStmt.executeInsert();
		}catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
	}
//END INSERTS
	
	
//UPDATES	
	public int updateGrade(Integer id, String grade) {
		ContentValues contentValues = new ContentValues();
		contentValues.put("grade", grade);
		return TusNotasModel.db.update(TusNotasModel.GRADE_TABLE_NAME, contentValues, "id = ?" , new String[]{id.toString()});
	}
		
	public int updateGrade(Grade grade) {
		ContentValues contentValues = new ContentValues();
		contentValues.put("grade", grade.getGrade());
		return TusNotasModel.db.update(TusNotasModel.GRADE_TABLE_NAME, contentValues, "id = ?" , new String[]{grade.getId().toString()});
	}
//END UPDATES
		
	
	
//DELETES
	public void deleteAllGrades() {
		TusNotasModel.db.delete(TusNotasModel.GRADE_TABLE_NAME, null, null);
	}
	
	public void deleteGradeById(Integer id) {
		TusNotasModel.db.delete(TusNotasModel.GRADE_TABLE_NAME, "id = "+id, null);
	}
//END DELETES	
	
	
//SELECTS
	public List<Grade> selectAllGrades(){
		List<Grade> grades = new ArrayList<Grade>();
		Grade grade = null;
		Cursor cursor = TusNotasModel.db.query(TusNotasModel.GRADE_TABLE_NAME, new String[] { "id", "activity", "grade", "courseId" },
				null, null, null, null, "id ASC");
		
		if (cursor.moveToFirst()) {
			do {
				grade = new Grade();
				grade.setId(cursor.getInt(0));
				grade.setActivity(cursor.getString(1));
				grade.setGrade(cursor.getString(2));
				grade.setCourseId(cursor.getInt(3));
				grades.add(grade);
			} while (cursor.moveToNext());
		}
		if (cursor != null && !cursor.isClosed()) {
			cursor.close();
		}
		return grades;
	}
	
	public List<Grade> selectGradesByCourse(Integer courseId) {
		List<Grade> grades = new ArrayList<Grade>();
		Grade grade = null;
		Cursor cursor = TusNotasModel.db.query(TusNotasModel.GRADE_TABLE_NAME, new String[] { "id", "activity", "grade", "courseId" },
				"courseId ='" + courseId +"'", null, null, null, "id ASC");
		
		if (cursor.moveToFirst()) {
			do {
				grade = new Grade();
				grade.setId(cursor.getInt(0));
				grade.setActivity(cursor.getString(1));
				grade.setGrade(cursor.getString(2));
				grade.setCourseId(cursor.getInt(3));
				grades.add(grade);
			} while (cursor.moveToNext());
		}
		if (cursor != null && !cursor.isClosed()) {
			cursor.close();
		}
		return grades;
	}
	
	public Grade selectGradesByCourseAndActivity(Integer courseId, String activity) {
		Grade grade = null;
		Cursor cursor = TusNotasModel.db.query(TusNotasModel.GRADE_TABLE_NAME, new String[] { "id", "activity", "grade", "courseId" },
				"courseId ='" + courseId +"' and activity ='" + activity +"'", null, null, null, "id ASC", "1");
		
		if (cursor.moveToFirst()) {
			do {
				grade = new Grade();
				grade.setId(cursor.getInt(0));
				grade.setActivity(cursor.getString(1));
				grade.setGrade(cursor.getString(2));
				grade.setCourseId(cursor.getInt(3));
				
			} while (cursor.moveToNext());
		}
		if (cursor != null && !cursor.isClosed()) {
			cursor.close();
		}
		return grade;
	}
//END SELECTS
}
